//
//  ViewController.m
//  21_NSURLRequestAndResponse
//
//  Created by 徐昊 on 16/8/11.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // NSURL负责请求的地址
    NSURL *baiduURL = [NSURL URLWithString:@"http://www.baidu.com/xuhao?xuhao:23"];
    NSLog(@"baidu : %@", baiduURL);
    // 1. 完整的URL对应的字符串
    NSLog(@"baiduURL.absoluteString : %@", baiduURL.absoluteString);
    // 2. host : 主机地址 / 域名
    NSLog(@"baiduURL.host : %@", baiduURL.host);
    // 3. path : 主机地址 / 域名后对应的路径
    NSLog(@"baiduURL.path : %@", baiduURL.path);
    // 4. query : ? 以后的内容
    NSLog(@"baiduURL.query : %@", baiduURL.query);
    // 5. scheme : URL所遵循的协议
    NSLog(@"baiduURL.scheme : %@", baiduURL.scheme);
    // 6. 除了协议的部分
    NSLog(@"%@", baiduURL.resourceSpecifier);
    // 不可变请求 和 可变请求
    // 1. NSURLRequest
    NSURLRequest *baiduRequest = [NSURLRequest requestWithURL:baiduURL];
    // 什么都不能改, 只能获取
    // 2. NSMutableURLRequest
    // 一般处理网络请求, 都是用可变NSMutableURLRequest
    NSMutableURLRequest *baiduMutableRequest = [NSMutableURLRequest requestWithURL:baiduURL];
    // 1> 超时时间, 一个请求持续多久, 会失败
    baiduMutableRequest.timeoutInterval = 60;
    // 2> HTTPMethod 请求的姿势 GET POST PUT DELETE
    baiduMutableRequest.HTTPMethod = @"GET";
    // 3> allHTTPHeaderFields 设置, 网络请求头
    baiduMutableRequest.allHTTPHeaderFields = @{@"User-Agent" : @"iOS 10.0 / iPhone8.1"};
    // 根据字典特性, 相同Key情况下, 后填入的值, 会覆盖前面的值
    [baiduMutableRequest setValue:@"iOS 10.0 / iPhone9.1" forHTTPHeaderField:@"User-Agent"];
    [baiduMutableRequest valueForHTTPHeaderField:@"User-Agent"];
    NSLog(@"%@", baiduMutableRequest.allHTTPHeaderFields);
    // 4> HTTPBody (一般POST请求时, 才会用)
    // 一般情况下, 都是字典/字符串转换成的Data
    baiduMutableRequest.HTTPBody = [NSData data];
    
    // 3. NSURLResponse 响应
//    NSURLResponse
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:baiduURL statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"date" : [NSDate date]}];
    // 1> statusCode 状态码
//    response.statusCode;
    // 2> allHeaderFiels 响应头
    // 内容格式 编码
    // Content - Type : application/json; charset:utf-8
    // 时间 日期
    // 缓存的一些内容
    NSLog(@"%@", response.allHeaderFields);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
